﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPIWareMVVM.ViewModels;
using DataAccess;
using System.Windows.Data;
using System.Windows;

namespace SPIWareMVVM.Views.Commands
{
    public class AddNewContactCommand : BaseCommand
    {
        public AddNewContactCommand(BaseViewModel sender) : base(sender) { }

        public override void Execute(object parameter)
        {
            //CustomerEdit panel = (CustomerEdit)parameter;
            //if (panel != null)
            //{
            //    ContactInfo ci = new ContactInfo();
            //    ((Window1)((System.Windows.Controls.DockPanel)((CustomerEdit)panel).Parent).Parent).customersList.SelectedCustomer.ContactInfos.Add(ci);
            //    //panel.lbContactData.SelectedItem = ci;

            //    //if (ApplicationData.SelectedCustomer != null)
            //    //{
            ////        Window1 mainWindow = (Window1)((System.Windows.Controls.DockPanel)panel.Parent).Parent;
            ////        if (mainWindow != null)
            ////        {
            ////            ApplicationViewModel appVM = (ApplicationViewModel)((ObjectDataProvider)mainWindow.Resources["applicationVM"]).ObjectInstance;
            ////            if (appVM != null)
            ////            {
            ////                ContactInfo ci = ApplicationData.NewContact(appVM);
            ////                panel.lbContactData.ItemsSource = appVM.SelectedCustomer.ContactInfos;
            ////                panel.lbContactData.SelectedItem = ci;
            ////                panel.Visibility = System.Windows.Visibility.Visible;
            ////            }
            ////        }
            //    //}
            //}

            CustomerList list = (CustomerList)parameter;
            ContactInfo ci = new ContactInfo();
            list.SelectedCustomer.ContactInfos.Add(ci);
            list.lbContactData.ItemsSource = list.SelectedCustomer.ocContactInfos;
            list.lbContactData.SelectedItem = ci;

            List<Type> types = new List<Type> { typeof(Customer) };
            ApplicationData.ClearAllExcept(types);

            mainWindow = (Window1)Window.GetWindow(list);
            mainWindow.HideLists();

            if (list.Visibility == System.Windows.Visibility.Collapsed) list.Visibility = System.Windows.Visibility.Visible;
            if (list.dpCustomerEdit.Visibility == Visibility.Collapsed) list.dpCustomerEdit.Visibility = Visibility.Visible;
            if (list.dpAddressEdit.Visibility == Visibility.Collapsed) list.dpAddressEdit.Visibility = Visibility.Visible;
        }
    }
}
